package org.example;

import io.grpc.stub.StreamObserver;
import org.keyvalue.KeyValueServiceOuterClass.*;
import org.keyvalue.KeyValueServiceGrpc;

import java.util.HashMap;
import java.util.Map;

public class KeyValueServiceImpl extends KeyValueServiceGrpc.KeyValueServiceImplBase {
    private Map<String, String> keyValueStore = new HashMap<>();

    @Override
    public void get(GetRequest request, StreamObserver<GetResponse> responseObserver) {
        String key = request.getKey();
        String value = keyValueStore.get(key);
        GetResponse response = GetResponse.newBuilder().setValue(value).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public void put(PutRequest request, StreamObserver<PutResponse> responseObserver) {
        String key = request.getKey();
        String value = request.getValue();
        keyValueStore.put(key, value);
        PutResponse response = PutResponse.newBuilder().setSuccess(true).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
